/*
	This file is part of AVRCppLib.

    AVRCppLib is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AVRCppLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AVRCppLib.  If not, see <http://www.gnu.org/licenses/>.

    Copyright (C) Maxime Lemonnier 2007-2011
 */

#ifndef RegexTest_H_
#define RegexTest_H_

#include "TestAssert.h"
#include "Utils/Utils.h"
#include "RegularExpressions/regexp/regexp.h"
#include "RegularExpressions/RegularExpression.h"
#include "RegularExpressions/Result.h"

using RegularExpressions::RegularExpression;
using RegularExpressions::Result;


class RegexTest : public TestCase<RegexTest>
{

public:

	RegexTest(const prog_mem * testMethodName_P, TestCase<RegexTest>::TestMethod testMethod) :
	TestCase<RegexTest>(testMethodName_P, testMethod),
	simpleRegex(PROG_MEM_STRING("group_1_a")),
	groupsRegex(PROG_MEM_STRING("(group_1_[a-z]).*(2_b)")),
	text(PROG_MEM_STRING("group_1_a group_2_b"))
	{

	}

	bool testregexp()
	{

	    regexp* compiled = regcomp(simpleRegex);

	    int retval = regexec(compiled, text);

	    bool result = AVRUNIT_ASSERT_EQUALS_LONG(retval, 1);

	    size_t matchTextLenght = compiled->endp[0] - compiled->startp[0];

	    result &= AVRUNIT_ASSERT_EQUALS_LONG(matchTextLenght, 9);

	    free(compiled);

	    compiled = regcomp(groupsRegex);

	    retval = regexec(compiled, text);

	    result &= AVRUNIT_ASSERT_EQUALS_LONG(retval, 1);

	    size_t group0Lenght = compiled->endp[0] - compiled->startp[0];

	    size_t group1Lenght = compiled->endp[1] - compiled->startp[1];

	    size_t group2Lenght = compiled->endp[2] - compiled->startp[2];

	    result &= AVRUNIT_ASSERT_EQUALS_LONG(group0Lenght, strlen(text));

	    result &= AVRUNIT_ASSERT_EQUALS_LONG(group1Lenght, 9);

	    result &= AVRUNIT_ASSERT_EQUALS_LONG(group2Lenght, 3);

	    free(compiled);

	    return result;
	}


	bool testRegularExpression()
	{

		RegularExpression simpleRe(simpleRegex);

		Result * res = simpleRe.parse(text);

		bool result = AVRUNIT_ASSERT(!isNull(res));

		result &= AVRUNIT_ASSERT_EQUALS_LONG(res->getGroup(0)->lenght, 9);

	    delete res;

		RegularExpression groupsRe(groupsRegex);

		res = groupsRe.parse(text);

		result &= AVRUNIT_ASSERT(!isNull(res));

		result &= AVRUNIT_ASSERT_EQUALS_LONG(res->getGroup(0)->lenght, strlen(text));

		result &= AVRUNIT_ASSERT_EQUALS_LONG(res->getGroup(1)->lenght, 9);

		result &= AVRUNIT_ASSERT_EQUALS_LONG(res->getGroup(2)->lenght, 3);

	    delete res;

	    return result;
	}

protected:
	const prog_mem * simpleRegex;
	const prog_mem * groupsRegex;
	const prog_mem * text;

};

#endif /* RegexTest_H_ */
